<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2024/5/25
  Time: 9:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- jQuery 库 -->
    <script src="../../js/jquery-3.5.1.slim.min.js"></script>
    <script src="../../js/axios.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <title>注册页面</title>
    <style>
        body {
            background: #f8f9fa;
        }

        .inputfile {
            opacity: 0;
            position: absolute;
            z-index: -1;
        }
        .container {
            max-width: 400px;
            margin: 20px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>

<body>
<div class="container">
    <h2 class="text-center mb-4">注册</h2>
    <div class="text-center mb-4">
        <img src="../../img/Avatar.png" alt="头像" class="rounded-circle" id="Avatar" style="width: 100px; height: 100px;">
    </div>
    <form>
        <div class="form-group">
            <label for="Account">账号</label>
            <input type="text" class="form-control" id="Account" placeholder="请输入账号" required>
        </div>
        <div class="form-group">
            <label for="Password">密码</label>
            <input type="password" class="form-control" id="Password" placeholder="请输入密码" required>
        </div>
        <div class="form-group">
            <label for="RePassword">重复密码</label>
            <input type="password" class="form-control" id="RePassword" placeholder="请再次输入密码"
                   oninput="validatePassword()" required>
            <small id="passwordHelp" class="form-text text-muted"></small>
        </div>
        <div class="input-group mb-3">
            <div class="input-group-prepend">
                <div class="input-group-text">
                    <input type="checkbox" aria-label="Checkbox for following text input" name="sex" id="nan" value="男">男
                    <input type="checkbox" aria-label="Checkbox for following text input" name="sex" id="nv" value="女">女
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="Email1">电子邮箱</label>
            <input type="email" class="form-control" id="Email1" aria-describedby="emailHelp">
        </div>
        <div class="form-group">
            <input type="file" id="file" class="inputfile" onchange="changepic(this)"
                   accept="image/png, image/jpeg, image/gif, image/jpg">
            <label for="file" class="btn btn-primary">上传头像</label>
        </div>
        <button type="button" class="btn btn-primary btn-block" onclick="register()" id="bt1">注册</button>
    </form>
    <p style="text-align: right;margin-top: 2%;">去<a href="login.jsp">登录</a></p>
</div>


<script>
    function validatePassword() {
        const password = document.getElementById('Password');
        const repassword = document.getElementById('RePassword');
        const passwordHelp = document.getElementById('passwordHelp');
        if (password.value !== repassword.value) {
            passwordHelp.textContent = '两次密码输入不一致';
            document.getElementById("bt1").disabled = true;

        } else {
            passwordHelp.textContent = '';
            document.getElementById("bt1").disabled = false;

        }
    }
    //上传头像
    function changepic(input) {
        const avatar = document.getElementById('Avatar');
        const reader = new FileReader();
        reader.onload = function (e) {
            avatar.src = e.target.result;
        };
        reader.readAsDataURL(input.files[0]);
    }

    function register() {
        let account = document.getElementById('Account').value;
        let password = document.getElementById('Password').value;
        let gender = document.querySelector('input[name="sex"]:checked');
        let email = document.getElementById('Email1').value;

        if (account && password) {
            axios({
                url: 'http://localhost:8080/user',
                method: 'post',
                data: {
                    username: account,
                    password: password,
                    gender: gender,
                    email: email
                }
            }).then(result => {
                console.log(result.data)
                if (result.data !== 500)
                    alert('注册成功');
                else
                    alert(result)
            })

            // 通常这里会有跳转到登录页面或首页的逻辑
        } else {
            alert('请填写账号和密码');
        }
    }
</script>
</body>

</html>
